package com.qf.springmvc02.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author nk
 * @date 2022/5/5 21:32
 */
@Controller
public class FileLoadController {
    @RequestMapping("/uploader")
    public String loader(MultipartFile uploader, String name, Model model, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("/");
        File uploaderDir = new File(realPath, "uploader");
        if (!uploaderDir.exists()) {
            uploaderDir.mkdirs();
        }
        String filename = uploader.getOriginalFilename();
        String suffix = filename.substring(filename.indexOf("."));
        if (suffix.equalsIgnoreCase(".jpg")
                || suffix.equalsIgnoreCase(".png")
                || suffix.equalsIgnoreCase("bmp")
                || suffix.equalsIgnoreCase(".gif")) {
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "") + suffix;
            uploader.transferTo(new File(uploaderDir, filename));
            model.addAttribute("msg", "文件上传成功");
            String[] filesNames = uploaderDir.list();
            model.addAttribute("filesNames", filesNames);
            return "welcome";
        }else {
            model.addAttribute("error","上传的不是图片格式！");
            return "uploader";
        }
    }
    @RequestMapping("/uploader2")
    public String loader2(MultipartFile[] uploaderFiles, String name, Model model, HttpServletRequest request) throws IOException {
        String realPath = request.getServletContext().getRealPath("/");
        File uploaderDir = new File(realPath, "uploader");
        if (!uploaderDir.exists()) {
            uploaderDir.mkdirs();
        }
        for (MultipartFile uploader : uploaderFiles) {
            String filename = uploader.getOriginalFilename();
            String suffix = filename.substring(filename.indexOf("."));
            if (suffix.equalsIgnoreCase(".jpg")
                    || suffix.equalsIgnoreCase(".png")
                    || suffix.equalsIgnoreCase("bmp")
                    || suffix.equalsIgnoreCase(".gif")) {
                String uuid = UUID.randomUUID().toString();
                uuid = uuid.replace("-", "") + suffix;
                uploader.transferTo(new File(uploaderDir, filename));
                model.addAttribute("msg", "文件上传成功");
                String[] filesNames = uploaderDir.list();
                model.addAttribute("filesNames", filesNames);

            }else {
                model.addAttribute("error","上传的不是图片格式！");
                return "uploader";
            }
        }
        return "welcome";
    }

    @RequestMapping("/downloader")
    public void downloader(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("content-disposition","attachment;filename="+filename);
        String realPath = request.getServletContext().getRealPath("/uploader");
        File file = new File(realPath, filename);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.close();
    }
}
